/**
 * 用于拓展 swagger-bootstrap-ui v1.8.7
 * https://gitee.com/xiaoym/swagger-bootstrap-ui
 **/
(function($, window){
  window.swaggerUiExtend = {
    historyDivHtml:"",
    timeout: 300,
    clickHistory: function(method, url){
      $(".menuLi").each(function(){
        var $this = $(this);
        var data = $this.data();
        if(data.data){
          data = data.data;
          if(data.methodType === method.toUpperCase() && data.url === url){
            $this.trigger("click");
            return false;
          }
        }
      });
    }
  };
  $(document).ajaxSuccess(function(event, request, settings){
    if(settings.url.indexOf("v2/api-docs") >= 0){
      setTimeout(function () {
        try {
          var historyDiv = $("#history_div");
          if(historyDiv.length === 0){
            historyDiv = $("<div id='history_div' class='tabs-container' style='width:99%;margin:0px auto;'></div>")
              .appendTo("#mainTabContent");
            historyDiv.append(
              "<div style='margin-top: 15px;' class='swbu-main'>" +
              "  <table><thead><tr><td colspan='6' style='text-align:center;'>Update History</td></tr></thead><tbody></tbody></table>" +
              "</div>"
            );
          }
          var table = historyDiv.find("table>tbody").html("");
          var data = JSON.parse(request.responseText);
          var paths = data.paths;
          var histories = [];
          for( var uri in paths ){
            var path = paths[uri];
            for(var method in path){
              var info = path[method];
              var api = {method:method, uri:uri, tag:info.tags[0], summary:info.summary, auth:info.AUTH, update:info.UPDATE};
              histories.push(api);
            }
          }
          histories.sort(function (a, b) {
            var au = a.update;
            var bu = b.update;
            if(!au && !bu) return 0;
            return au > bu ? -1 : 1;
          });
          var size = 10;
          var color = 255 / 10;
          for(var i = 0 ; i < size && i < histories.length; i++){
            var api = histories[i];
            table.append("<tr><td style='min-width:85px;display:table-cell' class='menu-url-"+api.method+"'>"+ api.method.toUpperCase()+"</td>" +
              "<td><a href=\"javascript:swaggerUiExtend.clickHistory('"+api.method+ "','"+api.uri+ "')\">"+api.uri+"</a></td>" +
              "<td>"+api.summary+"</td>" +
              "<td>"+api.tag+"</td>" +
              "<td>"+(api.auth?api.auth:"")+"</td>" +
              "<td style='color:rgb("+(255 - color * i)+", 0, 0)'>"+(api.update?api.update:"")+"</td></tr>");
          }
          window.swaggerUiExtend.historyDivHtml = $("#history_div").prop("outerHTML");
          $("#menu>li").eq(0).on("click",function(){
            setTimeout(function () {
              $("#mainTabContent").append(window.swaggerUiExtend.historyDivHtml);
            }, window.swaggerUiExtend.timeout);
          });
        }catch(e){}
      },  window.swaggerUiExtend.timeout); // 比 SwaggerBootstrapUi.prototype.createDescriptionElement 中的异步时间长即可
    }
  });

})(jQuery, window);